package com.vdf.sisconsolid.persistencemodule.converter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.vdf.sisconsolid.persistencemodule.dto.ConsolidationDTO;
import com.vdf.sisconsolid.persistencemodule.model.Address;
import com.vdf.sisconsolid.persistencemodule.model.Consolidation;
import com.vdf.sisconsolid.persistencemodule.model.Person;
import com.vdf.sisconsolid.persistencemodule.model.Tracing;
import com.vdf.sisconsolid.persistencemodule.model.TracingType;
import com.vdf.sisconsolid.util.CacheSC;
import com.vdf.sisconsolid.util.UtilSC;

public class ConsolidationConverter {

	public static Consolidation toEntity(ConsolidationDTO cDTO) {

		// consolidation
		Consolidation consolidation = new Consolidation();
		consolidation.setId(cDTO.getIdConsolidation());
		consolidation.setAttendedChurch(cDTO.getAttendedChurch());
		if (cDTO.getAttendedChurch()) {
			consolidation.setWhichChurch(cDTO.getWhichChurch());
		}
		consolidation.setAttendCell(cDTO.getAttendCell());
		consolidation.setContactTime(cDTO.getContactTime());
		consolidation.setDate(cDTO.getDate());
		consolidation.setInvitedBy(cDTO.getInvitedBy());
		consolidation.setNeed(cDTO.getNeed());
		consolidation.setObservation(cDTO.getObservation());
		consolidation.setOccupation(cDTO.getOccupation());
		consolidation.setYears(cDTO.getYears());
		consolidation.setVisitingDay(cDTO.getVisitingDay());
		consolidation.setState((byte) 0);

		if (cDTO.getAttendCell()) {
			consolidation.setIdCell(cDTO.getIdCell());
		}
		consolidation.setIdAssignedTo(cDTO.getIdAssignedTo());
		consolidation.setIdNet(cDTO.getIdNet());
		consolidation.setIdService(cDTO.getIdService());
		consolidation.setIdTookData(cDTO.getIdTookData());

		Person person = new Person();
		person.setId(cDTO.getIdPerson());
		person.setBirthday(getDayOfBirth(cDTO.getBirthday(), cDTO.getYears()));
		person.setCivilStatus(cDTO.getCivilStatus());
		person.setEmail(cDTO.getEmail());
		person.setFirstName(cDTO.getFirstName());
		person.setLastName(cDTO.getLastName());
		person.setPersonalPhone(cDTO.getPersonalPhone());
		person.setWorkPhone(cDTO.getWorkPhone());
		person.setSex(cDTO.getSex());
		consolidation.setPerson(person);

		Address address = new Address();
		address.setId(cDTO.getIdAddress());
		address.setIdProvince(cDTO.getIdProvince());
		address.setIdCity(cDTO.getIdCity());
		address.setIdNeighborhood(cDTO.getIdNeighborhood());
		address.setIdStreet(cDTO.getIdStreet());
		address.setNumber(cDTO.getNumber());
		address.setFloor(cDTO.getFloor());
		address.setDepartment(cDTO.getDepartment());
		address.setLot(cDTO.getLot());
		address.setBlock(cDTO.getBlock());
		person.setAddress(address);

		return consolidation;
	}
	
	public static List<ConsolidationDTO> toDTOList(
			List<Consolidation> consolidations) {

		List<ConsolidationDTO> consolidationDTOList = new ArrayList<ConsolidationDTO>();
		for (Consolidation consolidation : consolidations) {
			consolidationDTOList.add(toDTO(consolidation));
		}
		return consolidationDTOList;
	}

	public static ConsolidationDTO toDTO(Consolidation consolidation) {

		// consolidation
		ConsolidationDTO cDTO = new ConsolidationDTO();
		cDTO.setIdConsolidation(consolidation.getId());
		cDTO.setAttendedChurch(consolidation.getAttendedChurch());
		cDTO.setWhichChurch(consolidation.getWhichChurch());
		cDTO.setAttendCell(consolidation.getAttendCell());
		cDTO.setContactTime(consolidation.getContactTime());
		cDTO.setDate(consolidation.getDate());
		cDTO.setInvitedBy(consolidation.getInvitedBy());
		cDTO.setNeed(consolidation.getNeed());
		cDTO.setObservation(consolidation.getObservation());
		cDTO.setOccupation(consolidation.getOccupation());
		cDTO.setYears(consolidation.getYears());
		cDTO.setVisitingDay(consolidation.getVisitingDay());

		if (consolidation.getCell() != null) {
			cDTO.setIdCell(consolidation.getCell().getId());
			cDTO.setCell(consolidation.getCell().getPrittyName());
		}
		cDTO.setIdAssignedTo(consolidation.getAssignedTo().getId());
		cDTO.setAssignedTo(consolidation.getAssignedTo().getPerson()
				.getPrittyName());
		cDTO.setIdNet(consolidation.getNet().getId());
		cDTO.setNet(consolidation.getNet().getName());
		cDTO.setIdService(consolidation.getService().getId());
		cDTO.setService(consolidation.getService().getName());
		cDTO.setIdTookData(consolidation.getTookData().getId());
		cDTO.setTookData(consolidation.getTookData().getPerson()
				.getPrittyName());

		Person person = consolidation.getPerson();
		cDTO.setIdPerson(person.getId());
		cDTO.setBirthday(getBirthday(person.getBirthday()));
		cDTO.setCivilStatus(person.getCivilStatus());
		cDTO.setCivilStatusDescr(CacheSC.getCivilStatus().get(
				cDTO.getCivilStatus()));
		cDTO.setEmail(person.getEmail());
		cDTO.setFirstName(person.getFirstName());
		cDTO.setLastName(person.getLastName());
		cDTO.setPersonalPhone(person.getPersonalPhone());
		cDTO.setWorkPhone(person.getWorkPhone());
		cDTO.setSex(person.getSex());

		Address address = person.getAddress();
		cDTO.setIdAddress(address.getId());
		cDTO.setIdProvince(address.getProvince().getId());
		cDTO.setProvince(address.getProvince().getName());
		cDTO.setIdCity(address.getCity().getId());
		cDTO.setCity(address.getCity().getName());
		if (address.getNeighborhood() != null) {
			cDTO.setIdNeighborhood(address.getNeighborhood().getId());
			cDTO.setNeighborhood(address.getNeighborhood().getName());
		}
		if (address.getStreet() != null) {
			cDTO.setIdStreet(address.getStreet().getId());
			cDTO.setStreet(address.getStreet().getName());
		}
		cDTO.setNumber(address.getNumber());
		cDTO.setFloor(address.getFloor());
		cDTO.setDepartment(address.getDepartment());
		cDTO.setLot(address.getLot());
		cDTO.setBlock(address.getBlock());
		String prittyAddress = UtilSC.generatePrittyAddress(cDTO.getStreet(),
				cDTO.getNumber(), cDTO.getFloor(), cDTO.getDepartment(),
				cDTO.getBlock(), cDTO.getLot(), cDTO.getNeighborhood(),
				cDTO.getProvince(), cDTO.getCity());
		cDTO.setPrittyAddress(prittyAddress);

		addTracings(cDTO, consolidation);

		return cDTO;
	}

	private static void addTracings(ConsolidationDTO cDTO,
			Consolidation consolidation) {
		List<Tracing> tracingList = consolidation.getTracings();
		for (TracingType tt : CacheSC.getTracingTypes()) {
			if (tt.getId().equals(1L)) { // PHV1
				for (Tracing tracing : tracingList) {
					if (tt.getId().equals(tracing.getTracingType().getId())) {
						cDTO.setPhv1Id(tracing.getId());
						if (tracing.getAttendedCell()) {
							cDTO.setIntegratedToCell(true);
						}
					}
				}
				cDTO.setPhv1IdTracingType(tt.getId());
				cDTO.setPhv1TracingType(tt.getName());
				if (cDTO.getPhv1Id() == null) {
					cDTO.setPhv1Owed(isOwed(tt.getDaysLimit(), cDTO.getDate()));
				}
			} else if (tt.getId().equals(2L)) { // V1
				for (Tracing tracing : tracingList) {
					if (tt.getId().equals(tracing.getTracingType().getId())) {
						cDTO.setV1Id(tracing.getId());
						if (tracing.getAttendedCell()) {
							cDTO.setIntegratedToCell(true);
						}
					}
				}
				cDTO.setV1IdTracingType(tt.getId());
				cDTO.setV1TracingType(tt.getName());
				if (cDTO.getV1Id() == null) {
					cDTO.setV1Owed(isOwed(tt.getDaysLimit(), cDTO.getDate()));
				}
			} else if (tt.getId().equals(3L)) { // PHV2
				for (Tracing tracing : tracingList) {
					if (tt.getId().equals(tracing.getTracingType().getId())) {
						cDTO.setPhv2Id(tracing.getId());
						if (tracing.getAttendedCell()) {
							cDTO.setIntegratedToCell(true);
						}
					}
				}
				cDTO.setPhv2IdTracingType(tt.getId());
				cDTO.setPhv2TracingType(tt.getName());
				if (cDTO.getPhv2Id() == null) {
					cDTO.setPhv2Owed(isOwed(tt.getDaysLimit(), cDTO.getDate()));
				}
			} else if (tt.getId().equals(4L)) { // V2
				for (Tracing tracing : tracingList) {
					if (tt.getId().equals(tracing.getTracingType().getId())) {
						cDTO.setV2Id(tracing.getId());
						if (tracing.getAttendedCell()) {
							cDTO.setIntegratedToCell(true);
						}
					}
				}
				cDTO.setV2IdTracingType(tt.getId());
				cDTO.setV2TracingType(tt.getName());
				if (cDTO.getV2Id() == null) {
					cDTO.setV2Owed(isOwed(tt.getDaysLimit(), cDTO.getDate()));
				}
			} else if (tt.getId().equals(5L)) { // V3
				for (Tracing tracing : tracingList) {
					if (tt.getId().equals(tracing.getTracingType().getId())) {
						cDTO.setV3Id(tracing.getId());
						if (tracing.getAttendedCell()) {
							cDTO.setIntegratedToCell(true);
						}
					}
				}
				cDTO.setV3IdTracingType(tt.getId());
				cDTO.setV3TracingType(tt.getName());
				if (cDTO.getV3Id() == null) {
					cDTO.setV3Owed(isOwed(tt.getDaysLimit(), cDTO.getDate()));
				}
			} else if (tt.getId().equals(6L)) { // V4
				for (Tracing tracing : tracingList) {
					if (tt.getId().equals(tracing.getTracingType().getId())) {
						cDTO.setV4Id(tracing.getId());
						if (tracing.getAttendedCell()) {
							cDTO.setIntegratedToCell(true);
						}
					}
				}
				cDTO.setV4IdTracingType(tt.getId());
				cDTO.setV4TracingType(tt.getName());
				if (cDTO.getV4Id() == null) {
					cDTO.setV4Owed(isOwed(tt.getDaysLimit(), cDTO.getDate()));
				}
			}
		}
	}

	private static boolean isOwed(byte daysLimit, Date consolidationDate) {
		Calendar calLimitDate = Calendar.getInstance();
		calLimitDate.setTime(consolidationDate);
		calLimitDate.add(Calendar.DATE, daysLimit);
		// if today is grater than the consolidation date more days limit, it is
		// owed
		return new Date().after(calLimitDate.getTime());
	}

	public static Date getDayOfBirth(String dayMonthOfBirth, int years) {

		Calendar today = Calendar.getInstance();
		int dayOfBirth = Integer.parseInt(dayMonthOfBirth.split("/")[0]);
		int monthOfBirth = Integer.parseInt(dayMonthOfBirth.split("/")[1]);
		int age = years;

		Calendar dob = Calendar.getInstance();
		dob.set(Calendar.DAY_OF_MONTH, dayOfBirth);
		dob.set(Calendar.MONTH, monthOfBirth - 1);

		if (today.get(Calendar.MONTH) < dob.get(Calendar.MONTH)) {
			age++;
		} else if (today.get(Calendar.MONTH) == dob.get(Calendar.MONTH)
				&& today.get(Calendar.DAY_OF_MONTH) < dob
						.get(Calendar.DAY_OF_MONTH)) {
			age++;
		}
		dob.set(Calendar.YEAR, today.get(Calendar.YEAR) - age);

		return dob.getTime();

	}

	public static String getBirthday(Date birthday) {

		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM");
		return sdf.format(birthday);
	}

	public static int getYears(Date dateOfBirth) {

		Calendar dob = Calendar.getInstance();
		dob.setTime(dateOfBirth);
		Calendar today = Calendar.getInstance();
		int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);
		if (today.get(Calendar.MONTH) < dob.get(Calendar.MONTH)) {
			age--;
		} else if (today.get(Calendar.MONTH) == dob.get(Calendar.MONTH)
				&& today.get(Calendar.DAY_OF_MONTH) < dob
						.get(Calendar.DAY_OF_MONTH)) {
			age--;
		}

		return age;

	}
}